<?php

namespace App\Http\Controllers\API;

use App\Repositories\ArticleRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class CartsAPIController extends ApiBaseController
{
    protected $model_name = 'App\Models\Cart';
    protected $model_load_array = [
    ];
    protected $search_field_array = [
    ];

    public function __construct()
    {
    }

    public function authLimit(&$model)
    {
        $user = Auth::user();
        $res = [['user_id',$user?$user->id:0]];
        return $res;
    }


    public function _after_get($data)
    {
        $goods_ids = [];

        foreach ($data as $v) {
            if(isset($goods_ids[$v->goods_id])){
                $_m = clone $v;
                $_arr = $goods_ids[$v->goods_id]->carts;
                $_m->color;
                array_push($_arr,$_m);
                $goods_ids[$v->goods_id]->carts = $_arr;
                unset($_arr);
            }else{
                $goods_ids[$v->goods_id] = clone $v->goods;
                $_m = clone $v;
                if(isset($_m->goods))
                    unset($_m->goods);
                $_m->color;
                $_array = [$_m];
                $goods_ids[$v->goods_id]->carts = $_array;
            }
        }
        $array = [];
        foreach ($goods_ids as $goods_id) {
            $array[] = $goods_id;
        }
        return $array;
        $data->load('goods');
        $list = [];
        foreach ($data as &$v) {
            $_data = $v->toArray();
            $_data['goods_title'] = $v->goods->title;
            $_data['goods_pic'] = $v->goods->pic;
            $_data['color_title'] = $v->goods->color->title;
            $_data['color_pic'] = $v->goods->color->pic;
            $_data['goods_price'] = $v->goods->price;
            $list[] = $_data;
        }
        return $list;
    }
}
